package com.yanqu.road.web.controller.usercenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.servercenter.AppIdPlatformPolicy;
import com.yanqu.road.entity.servercenter.PlatformPolicy;
import com.yanqu.road.entity.web.HttpGameWebParam;
import com.yanqu.road.server.manger.AppIdPlatformPolicyMgr;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.server.manger.AppIdPlatformPolicyMgr;
import com.yanqu.road.server.manger.PlatformPolicyMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.string.StringUtils;
import com.yanqu.road.utils.http.HttpHelper;
import com.yanqu.road.utils.string.StringUtils;
import com.yanqu.road.web.WebServer;
import com.yanqu.road.web.controller.BaseGameHttpServlet;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2021/11/29 18:41
 */
@WebServlet(urlPatterns = "/platformPolicySendClient")
public class PlatformPolicySendClient extends BaseGameHttpServlet {
    private static Logger logger = LogManager.getLogger(PlatformPolicySendClient.class.getName());

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("request platformPolicy");
        HttpGameWebParam httpGameWebParam = super.parseRequest(request);
        JSONObject paramObj = httpGameWebParam.getParamObj();
        String language = httpGameWebParam.getLanguage();
        int type;
        int channelId;
        String appId;
        try {
            type = paramObj.getIntValue("type");
            channelId = paramObj.getIntValue("channelId");
            appId = paramObj.getString("appId");
        } catch (Exception e) {
            logger.error("platformPolicy argument error:" + e);
            sendMessage(response, getResultObj(GameErrorCode.E_REQUEST_ARGUMENT_ERROR, language).toJSONString());
            return;
        }
        PlatformPolicy platformPolicy = PlatformPolicyMgr.getPlatformPolicy(channelId);
        String content = "";
        if (platformPolicy != null) {
            if (type == 1) {
                content = platformPolicy.getUserAgreement();
            } else if (type == 2) {
                content = platformPolicy.getPrivacyPolicy();
            }
        }

        //子渠道配置
        if (!StringUtils.isNullOrEmpty(appId)) {
            AppIdPlatformPolicy appIdPlatformPolicy = AppIdPlatformPolicyMgr.getAppIdPlatformPolicy(appId);
            if (appIdPlatformPolicy != null) {
                if (type == 1) {
                    content = appIdPlatformPolicy.getUserAgreement();
                } else if (type == 2) {
                    content = appIdPlatformPolicy.getPrivacyPolicy();
                }
            }
        }

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("content", content);
        String result = JSON.toJSONString(dataMap);
        sendMessage(response, result);
    }
}
